import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/changePassword',
    name: 'ChangePassword',
    component: () => import('@/views/changepassword'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: 'home',
        component: () => import('@/views/dashboard/index'),
        name: 'Home',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/enterprise',
    component: Layout,
    redirect: 'noRedirect',
    alwaysShow: true,
    meta: { title: '企业管理', icon: 'group', roles: ['admin', 'agent1', 'agent2', 'agent3'] },
    children: [
      {
        path: 'list',
        name: 'EnterpriseList',
        component: () => import('@/views/enterprise/list'),
        meta: { title: '企业列表', icon: 'list', roles: ['admin', 'agent1', 'agent2', 'agent3'] }
      },
      {
        path: 'regManagement',
        name: 'RegManagement',
        component: () => import('@/views/enterprise/regManagement'),
        meta: { title: '企业注册', icon: 'edit', roles: ['admin', 'agent1', 'agent2', 'agent3'] }
      },
      // {
      //   path: 'reportList',
      //   name: 'reportList',
      //   component: () => import('@/views/ent/reportList'),
      //   meta: { title: '诊断报告', icon: 'people', roles: ['admin', 'agent1', 'agent2', 'agent3'] }
      // },
      // {
      //   path: 'downloadExcel',
      //   name: 'downloadExcel',
      //   component: () => import('@/views/ent/downloadExcel'),
      //   meta: { title: '导出Excel', icon: 'people', roles: ['admin', 'agent1', 'agent2', 'agent3'] }
      // },
      // },
      {
        path: 'details',
        name: 'EnterpriseDetails',
        component: () => import('@/views/enterprise/details'),
        meta: { title: '企业详情', roles: ['admin', 'agent1', 'agent2', 'agent3'] },
        hidden: true
      }
      // {
      //   path: 'entReport',
      //   name: 'EntReport',
      //   component: () => import('@/views/ent/report'),
      //   meta: { title: '企业详情', roles: ['admin', 'agent1', 'agent2', 'agent3'] },
      //   hidden: true
      // },
      // {
      //   path: 'entReportAll',
      //   name: 'EntReportAll',
      //   component: () => import('@/views/ent/reportAll'),
      //   meta: { title: '企业详情', roles: ['admin', 'agent1', 'agent2', 'agent3'] },
      //   hidden: true
      // },
      // {
      //   path: 'reportPdf',
      //   name: 'reportPdf',
      //   component: () => import('@/views/ent/reportPdf'),
      //   meta: { title: '企业详情', roles: ['admin', 'agent1', 'agent2', 'agent3'] },
      //   hidden: true
      // },
      // {
      //   path: 'reportNewPdf',
      //   name: 'reportNewPdf',
      //   component: () => import('@/views/ent/reportNewPdf'),
      //   meta: { title: '企业详情', roles: ['admin', 'agent1', 'agent2', 'agent3'] },
      //   hidden: true
      // },
      // {
      //   path: 'manageUpdate',
      //   name: 'ManageUpdate',
      //   component: () => import('@/views/manage/update'),
      //   meta: { title: '修改管理员', roles: ['agent1', 'agent2', 'agent3'] },
      //   hidden: true
      // },
      // {
      //   path: 'wenjuan',
      //   name: 'wenjuan',
      //   component: () => import('@/views/ent/wenjuan'),
      //   meta: { title: '问卷调查', icon: 'people', roles: ['admin', 'agent1', 'agent2', 'agent3'] }
      // }
    ]
  },
  {
    path: '/submitted',
    component: Layout,
    redirect: 'noRedirect',
    alwaysShow: true,
    meta: { title: '数据填报', icon: 'data', roles: ['admin', 'agent1', 'agent2', 'agent3'] },
    children: [
      {
        path: 'un/list',
        name: 'SubmittedUnList',
        component: () => import('@/views/submitted/unList'),
        meta: { title: '正在填报', icon: 'edit', roles: ['admin', 'agent1', 'agent2', 'agent3'] }
      },
      {
        path: 'list',
        name: 'SubmittedList',
        component: () => import('@/views/submitted/list'),
        meta: { title: '已完成填报', icon: 'success', roles: ['admin', 'agent1', 'agent2', 'agent3'] }
      },
      {
        path: 'details',
        name: 'SubmittedDetails',
        component: () => import('@/views/submitted/details'),
        meta: { title: '填报详情', roles: ['admin', 'agent1', 'agent2', 'agent3'] },
        hidden: true
      }
      // {
      //   path: 'import',
      //   name: 'ImportList',
      //   component: () => import('@/views/submitted/import'),
      //   meta: { title: '导入数据', icon: 'flip', roles: ['agent1', 'agent2', 'agent3'] }
      // }
    ]
  },
  {
    path: '/diagnosis',
    component: Layout,
    redirect: 'noRedirect',
    alwaysShow: true,
    meta: { title: '诊断管理', icon: 'data-update', roles: ['agent1', 'agent2', 'agent3'] },
    children: [
      {
        path: 'not/create',
        name: 'DiagnosisNotCreate',
        component: () => import('@/views/diagnosis/noCreate'),
        meta: { title: '未诊断', icon: 'example', roles: ['agent1', 'agent2', 'agent3'] }
      },
      {
        path: 'already/create',
        name: 'DiagnosisAlreadyCreate',
        component: () => import('@/views/diagnosis/list'),
        meta: { title: '已诊断', icon: 'success', roles: ['agent1', 'agent2', 'agent3'] }
      },
      {
        path: 'excel',
        name: 'DiagnosisResultExcel',
        component: () => import('@/views/diagnosis/resultExcel'),
        meta: { title: '导出诊断结果', icon: 'link', roles: ['agent1', 'agent2', 'agent3'] }
      }
    ]
  },
  {
    path: '/report',
    component: Layout,
    redirect: 'noRedirect',
    alwaysShow: true,
    meta: { title: '报告管理', icon: 'form', roles: ['agent1', 'agent2', 'agent3'] },
    children: [
      {
        path: 'not/create',
        name: 'ReportNotCreate',
        component: () => import('@/views/report/noDownload'),
        meta: { title: '未下载', icon: 'node', roles: ['agent1', 'agent2', 'agent3'] }
      },
      {
        path: 'list',
        name: 'ReportList',
        component: () => import('@/views/report/list'),
        meta: { title: '诊断报告', icon: 'viewgallery', roles: ['agent1', 'agent2', 'agent3'] }
      },
      {
        path: 'pdf/:id/url',
        name: 'ReportPdf',
        component: () => import('@/views/report/pdf'),
        meta: { title: '报告详情', roles: ['agent1', 'agent2', 'agent3'], keepAlive: false, noCache: true },
        hidden: true
      },
      // {
      //   path: 'import2',
      //   name: 'ImportList2',
      //   component: () => import('@/views/report/import'),
      //   meta: { title: '导入数据', icon: 'viewgallery', roles: ['agent1', 'agent2', 'agent3'], keepAlive: true, noCache: false }
      // },
      {
        path: 'pdf2/url',
        name: 'ReportPdf2',
        component: () => import('@/views/report/pdf2'),
        meta: { title: '报告详情', roles: ['agent1', 'agent2', 'agent3'], noCache: false },
        hidden: true
      }
    ]
  },
  {
    path: '/agent',
    component: Layout,
    redirect: 'noRedirect',
    alwaysShow: true,
    meta: { title: '次级代理管理', icon: 'skill', roles: ['agent1', 'agent2'] },
    children: [
      {
        path: 'agentList',
        name: 'AgentList',
        component: () => import('@/views/agent/list'),
        meta: { title: '次级代理', icon: 'user', roles: ['agent1', 'agent2'] }
      },
      {
        path: 'agentAdd',
        name: 'AgentAdd',
        component: () => import('@/views/agent/add'),
        meta: { title: '增加次级代理', roles: ['agent1', 'agent2'] },
        hidden: true
      },
      {
        path: 'agentUpdate',
        name: 'AgentUpdate',
        component: () => import('@/views/agent/update'),
        meta: { title: '修改次级代理', roles: ['agent1', 'agent2'] },
        hidden: true
      },
      {
        path: 'viewReport',
        name: 'ViewReport',
        component: () => import('@/views/agent/viewReport'),
        meta: { title: '查看报告', icon: 'skill', roles: ['agent1', 'agent2'] }
      },
      {
        path: 'pdfAgent/:id/url',
        name: 'PdfAgent',
        component: () => import('@/views/agent/pdfAgent'),
        meta: { title: '报告详情', roles: ['agent1', 'agent2', 'agent3'], keepAlive: false, noCache: true },
        hidden: true
      }
    ]
  },
  // {
  //   path: '/questionnaire',
  //   component: Layout,
  //   redirect: 'noRedirect',
  //   meta: { title: '问卷调查', icon: 'form', roles: ['agent1', 'agent2', 'agent3'] },
  //   children: [
  //     {
  //       path: 'list',
  //       name: 'DiagnosisNotCreate',
  //       component: () => import('@/views/questionnaire/list'),
  //       meta: { title: '问卷调查', icon: 'example', roles: ['agent1', 'agent2', 'agent3'] }
  //     }
  //   ]
  // },
  // {
  //   path: '/system',
  //   component: Layout,
  //   redirect: 'noRedirect',
  //   alwaysShow: true,
  //   meta: { title: '系统设置', icon: 'skill', roles: ['agent1', 'agent2', 'agent3'] },
  //   children: [
  //     {
  //       path: 'manageList',
  //       name: 'ManageList',
  //       component: () => import('@/views/manage/list'),
  //       meta: { title: '账号管理列表', icon: 'user', roles: ['agent1', 'agent2', 'agent3'] }
  //     },
  //     {
  //       path: 'manageAdd',
  //       name: 'ManageAdd',
  //       component: () => import('@/views/manage/add'),
  //       meta: { title: '增加管理员', roles: ['agent1', 'agent2', 'agent3'] },
  //       hidden: true
  //     },
  //     {
  //       path: 'manageUpdate',
  //       name: 'ManageUpdate',
  //       component: () => import('@/views/manage/update'),
  //       meta: { title: '修改管理员', roles: ['agent1', 'agent2', 'agent3'] },
  //       hidden: true
  //     }
  //   ]
  // },
  {
    path: '/single',
    component: () => import('@/head/index'),
    hidden: true,
    children: [
      {
        path: 'report/pdf/:id/url',
        name: 'SingleReportPdf',
        component: () => import('@/views/report/pdf'),
        meta: { title: '报告详情', roles: ['agent1', 'agent2', 'agent3'] },
        hidden: true
      }
    ]
  }
  // 404 page must be placed at the end !!!
]

const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  base: '/agent/',
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
